﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Tour.Linq.TourDBML;
using System.IO;
public partial class AdminManager_Admin_LineManager_Add : AdminBasePage
{

    protected int _LineID = 0;

    protected override void OnInit(EventArgs e)
    {
        _LineID = Request["LineID"].ToInt();
        base.OnInit(e);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DisplayInfo();
        }
    }

    private void DisplayInfo()
    {
        DisplayLineInfo();
        ddlTourType.DataSource = TourStaticData.TreeTourType;
        DataBind();
    }

    private void DisplayLineInfo()
    {
        if (_LineID > 0)
        {
            TourLine line = TourContext.TourLine.SingleOrDefault(p => p.ID == _LineID);
            if (line != null)
            {
                txtTourTitle.Text = line.Title;
                ddlTourType.SelectedValue = line.TypeID.ToString();
                ddlColorStyle.SelectedValue = line.TitleFontColor;
                RadioButtonList1.SelectedValue = line.TitleFontBold.ToString().ToLower();
                txtPrice.Text = line.Price.ToString();
                txtStartPoint.Text = line.StartPoint;
                txtEndPoint.Text = line.EndPoint;
                if (!line.Picture.IsNullOrEmpty()) Image1.ImageUrl = TourStaticData.UploadPicPath + line.Picture;
                txtStandard.Text = line.Standard;
                txtPriceBarring.Text = line.PriceBarring;
                txtVisaService.Text = line.VisaService;
                txtLineContent.Text = line.LineContent;
                txtDefaultSort.Text = line.DefaultSort.ToString();
                txtHotSort.Text = line.HotSort.ToString();
                txtTypeSort.Text = line.TypeSort.ToString();
                txtSaleSort.Text = line.SaleSort.ToString();

            }
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        _ReturnUrl = "Admin_LineManager.aspx";
        if (CheckEnterInfo())
        {
            TourLine line;
            StreamWriter sm = new StreamWriter(Server.MapPath("~/sql.log"));
            TourContext.Log = sm;
            try
            {
                if (_LineID > 0)
                {
                    line = TourContext.TourLine.SingleOrDefault(p => p.ID == _LineID);
                    _Message = "修改线路成功";


                }
                else
                {
                    line = new TourLine();
                    _Message = "添加线路成功";

                }
                line.Title = txtTourTitle.Text;
                line.TypeID = ddlTourType.SelectedValue.ToInt();
                line.TitleFontColor = ddlColorStyle.SelectedValue;
                line.TitleFontBold = RadioButtonList1.SelectedValue.ToBoolean();
                line.Price = Convert.ToDecimal(txtPrice.Text);
                line.StartPoint = txtStartPoint.Text;
                line.EndPoint = txtEndPoint.Text;
                line.Standard = txtStandard.Text;
                line.PriceBarring = txtPriceBarring.Text;
                line.VisaService = txtVisaService.Text;
                line.LineContent = txtLineContent.Text;
                line.DefaultSort = txtDefaultSort.Text.ToInt();
                line.HotSort = txtHotSort.Text.ToInt();
                line.TypeSort = txtTypeSort.Text.ToInt();
                line.SaleSort = txtSaleSort.Text.ToInt();
                line.Hodometer = String.Empty;
                line.TourCaution = string.Empty;
                line.Tags = String.Empty;
                line.TraitRecommand = String.Empty;
                if (fileImage.HasFile)
                {
                    line.Picture = GetImageFileName();
                }
                if (_LineID <= 0)
                {
                    TourContext.TourLine.InsertOnSubmit(line);
                }
                TourContext.SubmitChanges();
            }
            catch(Exception ex)
            {
                sm.WriteLine(ex.Message);
                sm.Close();
                throw ex;
            }
        }
    }

    private string GetImageFileName()
    {
        string extension = Path.GetExtension(fileImage.PostedFile.FileName).ToLower();//取上传图片的扩展名
        string fileName = DateTime.Now.ToString("yyyyMMddhhmmss");//取当前年月日时分秒
        string path = Server.MapPath(TourStaticData.UploadPicPath) + fileName + extension;
        fileImage.PostedFile.SaveAs(path);
       
        return fileName + extension;
    }
}
